package com.example.UI.database;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;


import com.example.UI.entity.Course;

import java.util.ArrayList;
import java.util.List;


public class CourseMessage extends SQLiteOpenHelper {
    private static final String DB_NAME = "schools";
    private static final int DB_Version = 1;

    public CourseMessage(@Nullable Context context) {
        super(context, DB_NAME, null, DB_Version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TABLE = "CREATE TABLE courses (" +
                "courseName TEXT NOT NULL ," +
                "courseId TEXT PRIMARY KEY ," +
                "courseTeacher TEXT NOT NULL ," +
                "courseClass TEXT ," +
                "courseTime INT )";
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS courses");
        onCreate(db);
    }
    public List<Course> getAllCourses() {
        SQLiteDatabase db = this.getReadableDatabase();
        List<Course> courses = new ArrayList<>();
        String[] columns = { "courseName", "courseId", "courseTeacher", "courseClass", "courseTime" };
        Cursor cursor = null;
        try {
            cursor = db.query("courses", columns, null, null, null, null, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    String courseName = cursor.getString(cursor.getColumnIndex("courseName"));
                    String courseId = cursor.getString(cursor.getColumnIndex("courseId"));
                    String courseTeacher = cursor.getString(cursor.getColumnIndex("courseTeacher"));
                    String courseClass = cursor.getString(cursor.getColumnIndex("courseClass"));
                    int courseTime = cursor.getInt(cursor.getColumnIndex("courseTime"));
                    Course course = new Course(courseName, courseId, courseTeacher, courseClass, courseTime);
                    courses.add(course);
                }
            }
        } catch (Exception e) {
            Log.e("CourseMessage", "Error retrieving all courses", e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            db.close();
        }
        return courses;
    }

    public long addCourse(Course course) {
        long newRowId = -1; // 默认值，表示添加失败
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        try {
            contentValues.put("courseName", course.getCourseName());
            contentValues.put("courseId", course.getCourseId());
            contentValues.put("courseTeacher", course.getCourseTeacher());
            contentValues.put("courseClass", course.getCourseClass());
            contentValues.put("courseTime", course.getCourseTime());
            newRowId = db.insert("courses", null, contentValues);
        } catch (Exception e) {
            // 这里可以打印日志，或者根据您的需要处理异常
            e.printStackTrace();
        } finally {
            // 确保关闭数据库，如果您在其他地方也使用了db对象的话
            db.close();
        }
        return newRowId;
    }

}
